package com.zhangtong.excel;

import com.zhangtong.excel.entity.StaffWorktimeData;
import com.zhangtong.excel.filter.CommonFilter;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 主程序，执行方式：鼠标右键-->"Run WorktimeS....main()"
 */
public class WorktimeStatisticsApp {
    public static void main(String[] args) throws Exception {
        //一般只需要改这里的文件路径
        String excelFile = "/Users/ZZ/Desktop/工时统计（测试）/20210303160153z.xls.xlsx";

        new StatisticsGenerator()
                //设置姓名列
                .setNameColumn('F')
                //设置项目列
                .setProjectColumn('B')
                //设置工时列
                .setWorktimeColumn('J')
                .loadExcel(excelFile)
                //设置工作表名称，一般叫"工作表1"
                .openSheet("工作表1")
                //如果希望把"/td>"结尾的内容切掉，可以把下面这行的注释关掉
//                .addFilter(new CommonFilter("","/td>"))
                .doStatistic()
                //按员工-项目统计
                .byStaffAndProject()
                //保存上一步生成的结果到excel文件，文件路径自动生成，生成规则是原excel文件路径下+xxx后缀
                .smartSaveStatistics()
                //按项目-员工统计
                .byProjectAndStaff()
                .smartSaveStatistics()
                //仅按员工统计，即员工--该员工总工时
                .byStaff()
                .smartSaveStatistics()
                //仅按项目统计，即项目--该项目总工时
                .byProject()
                .smartSaveStatistics()
                //一定要关闭文件，要不会出现奇奇怪怪的错误
                .close();
    }


    static void debug(Map<String, StaffWorktimeData> statistics) {
        AtomicInteger staffCount = new AtomicInteger(0);
        statistics.forEach((name, data) -> {
            staffCount.incrementAndGet();
            AtomicInteger projectCount = new AtomicInteger(0);

            data.getStatisticsData().forEach((project, worktime) -> {
                projectCount.incrementAndGet();
                System.out.println(name + " - " + project + " : " + worktime.toPlainString());
            });
            System.out.println("=========total project: " + projectCount.get() + "=========");
            System.out.println();
            System.out.println();
        });
        System.out.println("total staff: " + staffCount.get());
    }
}
